/*----------------------------------------------------------------------------
* This unit test tests the function isGameOver(). 
*----------------------------------------------------------------------------*/

#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

// set NOISY_TEST to 0 to remove printfs from output
#define NOISY_TEST 0

int checkOver(struct gameState *state) {

	int i, j = 0, pnum = 0, cardnum = 0;
	int check = isGameOver(state); //check stores result that function returns
	
#if (NOISY_TEST == 1)
	printf("\t Details: isGameOver() returned %d. ", check);
#endif		

	pnum = state->supplyCount[province]; 
	
#if (NOISY_TEST == 1)
	printf("There are %d Province cards. \n", pnum);
#endif		

	//not we check to see if province cards is empty
	if(pnum == 0){
		assert(check == 1); //province cards is empty, function should have returned same result
#if (NOISY_TEST == 1)
		printf("There are 0 Province cards. \n");
#endif
		return check;
	}
	
	//check to see if 3 supply piles are at 0 
	for(i = 0; i < 25; i++) {
		
		cardnum = state->supplyCount[i];
		if(cardnum == 0)
		{
			j++;
		}
		if(j >= 3)
		{
			assert(check == 1);
#if (NOISY_TEST == 1)
			printf("There are %d empty supply piles. \n", j);
#endif
			return check; 
		}
	}
	

	
	//if we get to this stage, it means that game should not be over
	//assert that our function did return game is not over
	assert(check == 0);
#if (NOISY_TEST == 1)
	printf("Assert correct, game is not over. \n");
#endif	
	
	return check;
}

int main() {

	int i, j, k, numTests = 50;
	struct gameState G;
	
	SelectStream(2);
	PutSeed((long)1);	
	
    printf("TESTING isGameOver():\n");
	
	for(i = 0; i < numTests; i++) {
	
	 //reset game state with random bytes
         for (j = 0; j < sizeof(struct gameState); j++) {
            ((char*)&G)[j] = floor(Random() * 256);
        } 
	
		//assigning random numbers to supply piles
		//this test misses the fact that only 10 treasure cards can be chosen
		for(k = 0; k <= treasure_map; k++) {
			G.supplyCount[k] = (int)floor(Random() * 25); 
		}
		
		//check game state
		checkOver(&G);
	
	}
	
	printf("isGameOver(): PASS \n");
	
	return 0;
}